<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="6">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
        </a-col>
        <a-col :span="8" :offset="10">
          <a-input-search
            style="max-width: 300px; min-width: 150px;float: right;"
            v-model="queryParam.search"
            placeholder="请输入名称"
            @search="onSearch"
          />
        </a-col>
      </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="tag_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <span slot="action" slot-scope="text, item">
        <a v-action:delete @click="handleDelete(item)">删除</a>
      </span>
    </s-table>

    <ItemForm ref="form"  @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/goods/tag'
import { STable } from '@/components'

import ItemForm from './modules/ItemForm'

export default {
  name: 'Index',
  components: {
    STable,

    ItemForm
  },
  data () {
    return {
      // 分类列表
      tableData: [],
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '标签ID',
          dataIndex: 'tag_id'
        },
        {
          title: '标签名称',
          dataIndex: 'name'
        },
        {
          title: '添加时间',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  methods: {

    /**
     * 新增记录
     */
    handleAdd () {
      this.$refs.form.edit()
    },

    /**
     * 删除记录
     */
    handleDelete (item) {
      const self = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.deleted({ tag_id: item['tag_id'] })
            .then((result) => {
              self.$message.success(result.message, 1.5)
              self.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      console.log(this.$refs.table)
      this.$refs.table.refresh(bool)
    },
    onSearch () {
      this.handleRefresh(true)
    }

  }
}
</script>
